<?php

/**
 * Created by PhpStorm.
 * Author: GJX
 * Time: 2023/10/10 15:53
 * Desc:
 */
class Service_Api_Wallet extends Contract_Api_Product
{
    use Trait_Api_Wallet;

    /**
     * 类型ID
     */
    const TYPE_ID = 196;

    public function search(array $params = array()): array
    {
        $list = DB::select('id', 'amount', 'support_items', 'gift_amount', 'gift_jifen', 'gift_coupon_id')
            ->from('wallet')
            ->where('ishidden', '=', 0)
            ->execute()->as_array();
        foreach ($list as &$v) {
            $gift_info = [];
            if($v['gift_amount']){
                array_push($gift_info,'赠送'.$v['gift_amount'].'元；');
            }
            if($v['gift_jifen']){
                array_push($gift_info,'赠送'.$v['gift_jifen'].'积分；');
            }
            if($v['gift_coupon_id']){
                $coupon_list = DB::select('name')->from('coupon')->where("id IN({$v['gift_coupon_id']})")->execute()->as_array();
                $str = '';
                foreach ($coupon_list as &$coupon) {
                    $str.= '赠送一张【'.$coupon['name'].'】优惠券；';
                }
                array_push($gift_info,$str);
            }
            $v['gift_info'] = $gift_info;
        }

        return $list;
    }

    public function info(array $params = array()): array
    {
        $wallet = DB::select('id', 'webid', 'amount', 'support_items', 'gift_amount', 'gift_jifen', 'gift_coupon_id')
            ->from('wallet')
            ->where('id', '=', $params['wallet_id'])
            ->execute()->current();

        if($wallet['gift_coupon_id']){
            $wallet['coupon_list']  = DB::select('id', 'name')->from('coupon')->where("id IN({$wallet['gift_coupon_id']})")->execute()->as_array();
        }
        $wallet['total_amount'] = bcadd($wallet['amount'], $wallet['gift_amount'], 2);

        $product_name = '充值' . $wallet['amount'] . '元';
        $product_name.= $wallet['gift_amount'] > 0 ? '，赠送' . $wallet['gift_amount'] . '元' : '';
        $product_name.= $wallet['gift_jifen'] > 0 ? '，赠送' . $wallet['gift_jifen'] . '积分' : '';
        $product_name.= $wallet['gift_coupon_id']? '，赠送' . count($wallet['coupon_list']) . '张优惠券' : '';
        $wallet['product_name'] = $product_name;

        return $wallet;
    }

    public function query_condition(): array
    {
        $filter = [
            ['id' => 0, 'val' => '全部'],
            ['id' => 1, 'val' => '充值'],
            ['id' => 2, 'val' => '消费'],
            ['id' => 3, 'val' => '退款'],
            ['id' => 4, 'val' => '赠送'],
        ];

        return [
            'filter' => $filter
        ];
    }

    public function create_order(array $params = array()): array
    {
        $wallet      = $this->info($params);
        $member_info = $params['member_info'];

        $order = [
            'webid'           => $wallet['webid'],
            'memberid'        => $params['mid'],
            'typeid'          => self::TYPE_ID,
            'ordersn'         => Product::get_ordersn(self::TYPE_ID),
            'productname'     => $wallet['product_name'],
            'productautoid'   => $wallet['id'],
            'price'           => $wallet['amount'],
            'dingnum'         => 1,
            'linkman'         => $member_info['truename'] ?: $member_info['nickname'],
            'linktel'         => $member_info['mobile'],
            'addtime'         => time(),
            'paysource'       => $params['paysource']?:'uniH5',
            'mobile_source'   => $params['mobile_source'],
            'auto_close_time' => bcadd(time(), 600)
        ];

        $out = Model_Api_V2_Order::add_order($order, 'Model_Wallet', $params);
        if (!$out['status']) {
            throw new Exception($out['msg']);
        }

        $order_info = Model_Member_Order::order_info($order['ordersn']);
        $order_info['orderid'] = $order_info['id'];

        // 明细记录
        $this->wallet_order_log($order_info, $wallet);//下单时的赠送项

        // 预订事件
        Hook::trigger('order_created', $order_info);

        // 返回订单信息
        $filter_keys = array('id', 'orderid', 'ordersn', 'typeid', 'status');

        return $this->filter_data($filter_keys, $order_info);
    }

}